<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <!-- 注意：label决定当前选中的值 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { assignRoles } from '@/api/employees'
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    // 用户的id 用来查询当前用户的角色信息
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 保存当前角色信息
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
        console.log('保存角色', res)
        this.loadRoles()
      } catch (error) {
        console.log(error)
      }
      this.$emit('close')
    },
    // 获取页面 刷新
    async loadRoles() {
      // 取一个比较大的页码来一次模拟获取所有的数据
      try {
        const res = await getRoles({ page: 1, pagesize: 100 })
        // 保存所有角色
        this.list = res.data.rows
        const info = await getUserDetailById(this.employeeId)
        console.log('getUserDetailById...........', info)
        // 保存这个员工当前的已经有的角色
        this.roleIds = info.data.roleIds
      } catch (error) {
        console.log(error)
      }
    },
    // 点击取消 关闭弹层
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
